<template>
  <view class="goods-list-container">
    <view class="goods-list">
      <view class="goods-item" v-for="item in goodslist" :key="item.goods_id" @click="enterDetail(item.goods_id)">
        <view class="image-box">
          <image class="goods-image" :src="item.goods_big_logo||defaultPic" mode="widthFix"></image>
        </view>
        <view class="goods-info">
          <text class="goods-name">{{item.goods_name}}</text>
          <text class="goods-price">￥{{item.goods_price|toFixed}}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
        queryObj: {
          // 查询关键词
          query: '',
          // 商品分类Id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10
        },
        total: 0,
        goodslist: [],
        isLoading: false
      }
    },
    filters: {
      toFixed(val) {
        return Number(val).toFixed(2)
      }
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodslist()

    },
    methods: {
      async getGoodslist(cd) {

        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        // 3.2 请求失败
        if (res.meta.status !== 200) return uni.$showMsg()

        // 3.3 请求成功，为 data 中的数据赋值
        this.goodslist = [...this.goodslist, ...res.message.goods]
        this.total = res.message.total
        this.isLoading = false
        if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
          this.isLoading = true
        }
        cd&&cd()

      },
      enterDetail(goods_id) {
        uni.navigateTo({
          url: '../good_detail/good_detail?goods_id=' + goods_id
        })
      }

    },
    onPullDownRefresh() {
      console.log('下拉刷新了')
      this.queryObj.pagenum = 1
      this.total = 0
      this.isloading = false
      this.goodslist = []
      this.getGoodslist(()=>{ uni.stopPullDownRefresh() })
    },
    onReachBottom() {
      this.queryObj.pagenum += 1
      
      if (this.isLoading) return uni.showToast({
        title: '数据加载完毕',
        icon: 'none'
      })
      this.isLoading = true
      this.getGoodslist()
    },

  }
</script>

<style lang="scss">
  .goods-list-container {
    .goods-list {
      .goods-item {
        padding: 10px 5px;
        border-bottom: 1px solid #f0f0f0;
        display: flex;

        .image-box {
          margin-right: 10px;

          .goods-image {
            width: 100px;
          }
        }

        .goods-info {
          display: flex;
          flex-direction: column;
          justify-content: space-between;

          .goods-name {
            font-size: 13px;
          }

          .goods-price {
            color: #c00000;
            font-size: 16px;
          }
        }
      }
    }
  }
</style>
